package com.jeesite.modules.yzw.web;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.wzt.service.WztUserService;
import com.jeesite.modules.yzw.service.YzwService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "aht/topc")
public class AnhuiTongPcController extends BaseController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private WztUserService wztUserService;
    @Resource
    private YzwService yzwService;
    @RequestMapping(value = "index")
    public String pc(String moapToken,String moapIp,   HttpServletRequest request,
                     HttpServletResponse response) throws ServletException, IOException {
        String wzt_url = Global.getConfig("platform.wzt.url");
        String url = wzt_url+"/idp/restful/getIDPUserAttributes";
        JSONObject res = getJsonObject(moapToken, moapIp, url);
        if(res != null){
            Map data = (Map) res.get("data");
            Map user =(Map)  data.get("attributes");
            if(user != null){
                String userId = (String)user.get("userid");
                Map paramMap = new HashMap();
                paramMap.put("status", "0");
                paramMap.put("extendS3", userId);
                Map sys_user = wztUserService.findWztList(paramMap);
                if(sys_user != null){
                    String userName = (String) sys_user.get("login_code");
                    String ssoToken = UserUtils.getSsoToken(userName);
                    if (isHavingPortal((String) user.get("user_code"))) {
                        return REDIRECT + "/sso/" + userName + "/" + ssoToken + "?url=/a/index#" + url + "/a/home/homePage/workPortal";
                    } else {
                        return REDIRECT + "/sso/" + userName + "/" + ssoToken;
                    }
                }else {
                    request.setAttribute("message", "用户不存在，请联系管理员");
                    request.getRequestDispatcher("/error/403").forward(request, response);
                }
            }
        }else{
            request.setAttribute("message", "免登陆失败");
            request.getRequestDispatcher("/error/403").forward(request, response);
        }
        System.out.println("moapToken="+moapToken);
        System.out.println("moapIp="+moapIp);
        return "pc";
    }

    /**
     * 调用“政府工作人员统一身份认证平台” getIDPUserAttributes 获取用户信息
     * @param moapToken
     * @param moapIp
     * @param url
     * @return
     */
    private JSONObject getJsonObject(String moapToken, String moapIp, String url) {
        MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        postParameters.add("appId", "wzt");
        postParameters.add("tokenId",moapToken);
        postParameters.add("remoteIp", moapIp);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(postParameters, headers);
        //使用JSONObject，不需要创建实体类VO来接受返参，缺点是别人不知道里面有哪些字段,即不知道有那些key
        return restTemplate.postForObject(url, request, JSONObject.class);
    }

    private boolean isHavingPortal(String userCode) {
        Map map = new HashMap();
        map.put("userCode", userCode);
        map.put("menuName", "工作门户");
        return this.yzwService.findYzwMenu(map);
    }
}
